package com.cheer.ba.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.widget.ProgressWebView;

import butterknife.BindView;

/**
 * 功能：显示H5的界面,不带标题
 * 说明：
 */
public class CommH5Aty extends BaseCommAty {

    //进度条
    @BindView(R.id.id_webview)
    ProgressWebView webView;

    //错误url
    @BindView(R.id.id_error_ulr_txt)
    TextView mTxtErrorUrl;

    //哪个界面
    private int page = 0;

    //路径地址
    private String url;

    //是否支持缩放 true 支持
    private boolean isSupportZoom;

    @Override
    protected boolean isSupportSwipeback() {
        return false;
    }

    @Override
    protected boolean isStatusBarTint() {
        return false;
    }

    @Override
    protected boolean isTitleBarShow() {
        return false;
    }

    /**
     * 启动h5界面,不带标题
     */
    public static void startCommonH5(Activity activity, String url, boolean isSupportZoom) {
        Intent intent = new Intent(activity, CommH5Aty.class);
        intent.putExtra(IntentKey.General.KEY_URL, url);
        intent.putExtra(IntentKey.General.KEY_BOOLEAN, isSupportZoom);
        activity.startActivity(intent);
    }

    @Nullable
    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_comm_h5, null));
    }

    @Override
    public void initView() {
        page = getIntent().getIntExtra("page", Constants.DEFAULTVALUE);
        url = getIntent().getStringExtra(IntentKey.General.KEY_URL);
        isSupportZoom = getIntent().getBooleanExtra(IntentKey.General.KEY_BOOLEAN, false);

        webView.setVisibility(View.VISIBLE);
//        webView.getSettings().setJavaScriptEnabled(true);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        if (isSupportZoom) {
            //支持屏幕缩放
            settings.setSupportZoom(true);
            settings.setBuiltInZoomControls(true);
            //不显示webview缩放按钮
            settings.setDisplayZoomControls(false);
        }
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.setVerticalScrollBarEnabled(false);
        webView.setVerticalScrollbarOverlay(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setHorizontalScrollbarOverlay(false);
        webView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                if (url != null && url.startsWith("http://"))
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri
                            .parse(url)));
            }
        });
        if (!StringUtils.isEmpty(url) && (url.startsWith("http://") || url.startsWith("https://"))) {
            webView.loadUrl(url);
        } else {
            mTxtErrorUrl.setVisibility(View.VISIBLE);
            webView.setVisibility(View.GONE);
            if (StringUtils.isEmpty(url)) {
                mTxtErrorUrl.setText("跳转链接为空");
            } else {
                mTxtErrorUrl.setText(url);
            }
        }
    }

    @Override
    public void initData() {

    }
}
